package com.xl.juc.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列
 * @author Charley
 * @create 2021-01-24
 */
public class BlockQueue {
    public static void main(String[] args) throws InterruptedException {
        test4();
    }
    /**
     * 抛出异常队列
     */
    private static void  test1(){
        ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.add("a"));
        System.out.println(arrayBlockingQueue.add("b"));
        System.out.println(arrayBlockingQueue.add("c"));
        //Exception in thread "main" java.lang.IllegalStateException: Queue full
        //System.out.println(arrayBlockingQueue.add("d"));

        System.out.println("==============");

        System.out.println(arrayBlockingQueue.element());//检测对手元素
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        //xception in thread "main" java.util.NoSuchElementException
        // System.out.println(arrayBlockingQueue.remove());
    }

    /**
     * 有返回值，没有异常
     */
    public  static  void test2(){
        ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));

        //System.out.println(arrayBlockingQueue.offer("d"));//队列满了不抛出异常，返回false

        System.out.println("==============");

        System.out.println(arrayBlockingQueue.peek());//检测对手元素
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
      //  System.out.println(arrayBlockingQueue.poll());//队列满了不抛出异常，返回null

    }

    /**
     * 等待，阻塞（一直阻塞）
     */
    public  static  void test3() throws InterruptedException {
        ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        arrayBlockingQueue.put("a");
        arrayBlockingQueue.put("b");
        arrayBlockingQueue.put("c");
       // arrayBlockingQueue.put("d"); //队列没位置了，一直阻塞

      //取出
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
       // System.out.println(arrayBlockingQueue.take());

    }

    /**
     * 等待，阻塞（超时退出阻塞）
     * @throws InterruptedException
     */
    public  static  void test4() throws InterruptedException {
        ArrayBlockingQueue<Object> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("b"));
        System.out.println(arrayBlockingQueue.offer("c"));
      //  System.out.println(arrayBlockingQueue.offer("d",2, TimeUnit.SECONDS));//超时等待，就退出
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS));//超时等待，就退出
    }

}
